
<script setup lang="ts">
import {Plus, Upload} from '@element-plus/icons-vue'
// import type { TableColumnCtx } from 'element-plus'
import * as Api from '@/api/people'
import {ref,onMounted} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
const tableData =ref([])
let total = ref(380)
let pageSize = ref(25)
let currentPage = ref(1)
let id = ref("")
let username = ref("")
let role = ref("")
const getData =async ()=> {
  let result = await Api.page({
    "currentPage": currentPage.value,
    "pageSize": pageSize.value,
    "id": id.value,
    "username": username.value,
    "role": role.value,
  })
  if (result.code === 200){
    tableData.value = result.data.rows
    total.value = result.data.total
  }
}
const handleCurrentChange = (e) => {
  currentPage.value = e
  getData()
}
const handleSizeChange = (e)=>{
  pageSize.value = e
  getData()
}
onMounted(async () => {
  await getData()
})
const shuaxin =async ()=> {
  id.value='';
  username.value='';
  role.value=''
  let result = await Api.page({
    "id":'',
    "username":'',
    "role": '',
  })
  if (result.code === 200){
    tableData.value = result.data.rows
  }
}

//add
const form = ref({
  id :'',
  username: '', //调查问卷链接
  password: '', //标题
  gender: '', //图片地址
  role: '', //链接地址
  tel: '',
})
const dialogVisible = ref(false)
const drawer = ref(false)
const handleClose = (done: () => void) => {
  ElMessageBox.confirm('您确定取消操作?')
      .then(() => {
        done()
      })
      .catch(() => {
        // catch error
      })
}
let isAdd = ref(true)
const btuAdd = ()=> {
  dialogVisible.value=true
  form.value.id= ""
  form.value.username = ""
  form.value.password= ""
  form.value.gender = ""
  form.value.role = ""
  form.value.tel = ""
}
const add = async () => {
  let result = await Api.add({
    "id" :  form.value.id,
    "username": form.value.username,
    "password":form.value.password,
    "gender":form.value.gender,
    "role":form.value.role,
    "tel":form.value.tel
  })
  if(result.code === 200){
    getData()
    dialogVisible.value=false
    ElMessage.success(result.message)
  }else {
    ElMessage.error(result.message)
  }
}
const update = async (id) => {
  let result =await Api.update(id,{
    "id" :  form.value.id,
    "username": form.value.username,
    "password":form.value.password,
    "gender":form.value.gender,
    "role":form.value.role,
    "tel":form.value.tel
  })
  if (result.code === 200){
    await getData()
    dialogVisible.value=false
    ElMessage.success(result.message)
  }else {
    ElMessage.error(result.message)
  }
  isAdd.value = true
}
const btuUpdate = (data)=> {
  isAdd.value = false
  dialogVisible.value=true
  form.value.id = data.id
  form.value.username = data.username
  form.value.password = data.password
  form.value.gender = data.gender
  form.value.role = data.role
  form.value.tel = data.tel
}
const submit = ()=> {
  if (isAdd.value) {
    add()
  } else {
    update(form.value.username)
  }
}
const dele = async (id) => {
  console.log(id);
  await ElMessageBox.confirm('您确定要删除吗', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  }).then(async () => {
    let result = await Api.dele(id)
    if (result.code === 200) {
      getData()
      ElMessage.success(result.message)
    } else {
      ElMessage.error(result.message)
    }
  })
}
const value = ref('')

const options = [
  {
    value: '管理员',
    label: '管理员',
  },
  {
    value: '老师',
    label: '老师',
  },
]
const option = [
  {
    value: '男',
    label: '男',
  },
  {
    value: '女',
    label: '女',
  },
]
</script>

<template>
  <div style="position: absolute;top: 100px;width: 90%;height: 40px;border-bottom: solid 1px rgb(221.7, 222.6, 224.4);margin: auto">
    <el-row>
      <el-col :span="15">
        <el-button @click="btuAdd" style="border: solid 1px #4285F4;border-radius: 20px;width: 150px;color: #4285F4;background-color:rgb(216.8, 235.6, 255);" >
          新增用户<el-icon class="el-icon--right"><el-icon><Plus /></el-icon></el-icon>
        </el-button>
        <div class="grid-content ep-bg-purple" />
      </el-col>

      <el-col :span="9" style="display: flex;justify-content: center">
        <el-input
            v-model="id"
            style="width: 200px;padding-left: 5px;border-radius: 20px ;margin-right: 5px"
            placeholder="请输入id"
        />
        <el-input
            v-model="username"
            style="width: 200px;padding-left: 5px;border-radius: 20px ;margin-right: 5px"
            placeholder="请输入名字"
        />
        <el-select
            v-model="role"
            placeholder="角色"
            size="large"
            style="width: 180px;height: 20px;min-height: 30px;padding-left: 5px;margin-right: 5px"
        >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
        <el-button style="border: solid 1px #4285F4;border-radius: 20px;width: 20px;color: #4285F4;background-color:rgb(216.8, 235.6, 255);"@click="getData">
          <el-icon class="el-icon--right"><el-icon><Search /></el-icon></el-icon>
        </el-button>
        <el-button style="border: solid 1px #4285F4;border-radius: 20px;width: 20px;color: #4285F4;background-color:rgb(216.8, 235.6, 255);" @click="shuaxin">
          <el-icon ><el-icon><RefreshRight /></el-icon></el-icon>
        </el-button>
        <div class="grid-content ep-bg-purple" />
      </el-col>
    </el-row>


  </div>
  <div style="height: 88%;width: 100%;margin-top: 80px">
    <el-table
        :data="tableData"
        :default-sort="{ prop: 'date', order: 'descending' }"
        style="width: 100%;height: 95%"
    >
      <el-table-column fixed type="index" label="编号" width="120" />
      <el-table-column prop="id" label="id" width="120" />
      <el-table-column prop="username" label="姓名" width="180" />
<!--      <el-table-column prop="password" label="密码" width="180" />-->
      <el-table-column prop="gender" label="性别" width="180" />
      <el-table-column prop="role" label="角色" width="180" />
      <el-table-column prop="tel" label="联系电话" width="180" />
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="scope">
          <el-button
              link
              size="small"
              style="color: #4285F4"
              @click="btuUpdate(scope.row)"
          >
            编辑
          </el-button>
          <el-button
              link
              size="small"
              style="color: #4285F4"
              @click="dele(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[25, 50, 100, 200]"

        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 10px"
    />
  </div>
  <!--抽屉-->
  <el-dialog
      v-model="dialogVisible"
      :title="isAdd ? '新增:' : '修改:'"
      width="550"
      :before-close="handleClose"
  >
    <div style="width: 100% ; border-top: #6b778c 1px solid;margin-bottom: 50px"></div>
      <span>
        <el-form :model="form" label-width="auto" style="max-width: 600px;font-size: 20px">
    <el-form-item  label="账号:">
      <el-input v-model="form.id"  style="width: 400px" />
    </el-form-item>
          <el-form-item  label="名称:">
      <el-input  v-model="form.username" style="width: 400px"/>
    </el-form-item>
          <el-form-item label="密码:">
      <el-input v-model="form.password" style="width: 400px" />
    </el-form-item>
          <el-form-item  label="联系方式:">
      <el-input  v-model="form.tel" style="width: 400px"/>
    </el-form-item>
    <el-form-item label="性别:">
      <el-select
          v-model="form.gender"
          placeholder="性别"
          size="large"
          style="width: 400px;height: 20px;min-height: 30px;;margin-right: 5px"
      >
          <el-option
              v-for="item in option"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
    </el-form-item>
    <el-form-item label="角色:">
<!--      <el-input v-model="form.role" style="width: 400px"/>-->
      <el-select
          v-model="form.role"
          placeholder="角色"
          size="large"
          style="width: 400px;height: 20px;min-height: 30px;margin-right: 5px"
      >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
    </el-form-item>

     <el-form-item >
      <el-button @click="submit()" style="border: solid 1px #4285F4;border-radius: 20px;width: 100px;color: #4285F4;margin-left: 200px">提交</el-button>
    </el-form-item>
  </el-form>
      </span>
  </el-dialog>
</template>

<style scoped>
.avatar-uploader .avatar {
  width: 320px;
  height: 238px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.el-select--large .el-select__wrapper {
  font-size: 14px;
  gap: 6px;
  line-height: 24px;
  min-height: 32px;
  padding: 8px 16px;
}
.el-dialog__title{
  font-size: 35px;
  font-weight:normal;
  color: black;
}
</style>